package deleteface.bank.jqsoft.cn.deleteface.setip;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;

import java.io.IOException;

import cn.jqsoft.bank.deleteface.R;
import deleteface.bank.jqsoft.cn.deleteface.base.LoadingUtil;
import okhttp3.ResponseBody;

public class Util {
    public static String decodeString(String strData)
    {

        strData = strData.replaceAll("&lt;", "<");
        strData = strData.replaceAll( "&gt;", ">");
        strData = strData.replaceAll( "&apos;", "&apos;");
        strData = strData.replaceAll( "&quot;", "\"");
        strData = strData.replaceAll( "&amp;", "&");
        return strData;
    }
    public static String DecodeXmlJosn(ResponseBody responseBody) {
        try {
            String res = responseBody.string();
            if (res == null) {
//                callBack.onError("请求发生未知错误");
                return "";
            }

            if (res.contains("{") && res.contains("}")) {
                int startIndex = res.indexOf("{");
                int endIndex = res.lastIndexOf("}") + 1;
                String json = res.substring(startIndex, endIndex);
                json=decodeString(json);
                return json;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "";
//            callBack.onError(e.toString());
        }
        return "";
    }
    public static void showToast(Context context, String msg) {
        try {
            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void hideGifProgressDialog(Context context) {
        try {
            final Context context1 = ((ContextWrapper) context).getBaseContext();
            if (context1 instanceof Activity) {
                if (!((Activity) context1).isFinishing() && !((Activity) context1).isDestroyed()) {
                    ((Activity) context1).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            LoadingUtil.getInstance(context1).hide();
                        }
                    });
                }

            } else {//if the Context used wasnt an Activity, then dismiss it too
                LoadingUtil.getInstance(context).hide();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setDialogWidthHeight(Activity activity, Dialog dialog, float widthPercent, float heightPercent) {
        WindowManager m = activity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.height = (int) (d.getHeight() * heightPercent); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * widthPercent); // 宽度设置为屏幕的0.65
        dialogWindow.setAttributes(p);

    }

    public static void showGifProgressDialog(Context context) {

        try {
            LoadingUtil.getInstance(context).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showGifProgressDialogFalse(Context context) {

        try {
            LoadingUtil.getInstance(context).showFalse();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 监测是否联网
     */
    public static boolean isNetworkConnected(Context context){
        //判断上下文是不是空的
        //为啥要判断啊? 防止context是空的导致 报空指针异常
        if (context!=null){
            //获取连接管理器
            ConnectivityManager mConnectivityManager= (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            //获取网络状态mConnectivityManager.getActiveNetworkInfo();
            NetworkInfo mNnetNetworkInfo=mConnectivityManager.getActiveNetworkInfo();
            if (mNnetNetworkInfo!=null){
                //判断网络是否可用//如果可以用就是 true  不可用就是false
                return mNnetNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    // 去掉字符串首尾的空白符
    public static String trimString(String s) {
        if (s == null) {
            return "";
        } else {
            return s.trim();
        }
    }


    public static void gotoActivityWithBundle(Context context, Class targetActivityClass, Bundle bundle) {
        if (context == null || targetActivityClass == null) {
            return;
        } else {
            try {
                Intent intent = new Intent(context, targetActivityClass);
                if (bundle != null) {
                    intent.putExtras(bundle);
                }
                context.startActivity(intent);
                if (context instanceof Activity) {
                    Activity activity = (Activity) context;
                    activity.overridePendingTransition(R.anim.right_in, R.anim.right_out);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * MaterialDialog   普通对话框
     *
     * @param context               设备上下文
     * @param title                 标题
     * @param content               内容
     * @param negativeText          取消按钮的文字
     * @param negtiveClickListener  取消按钮的点击方法
     * @param positiveText          确定按钮的文字
     * @param positiveClickListener 确定按钮的点击方法
     * @param cancelable            点击对话框以外是否可以使对话框消失
     */
    public static void showMaterialDialog(Context context, String title, String content,
                                          String negativeText, MaterialDialog.SingleButtonCallback negtiveClickListener,
                                          String positiveText, MaterialDialog.SingleButtonCallback positiveClickListener,
                                          boolean cancelable) {
        MaterialDialog.Builder dialog = new MaterialDialog.Builder(context);
        dialog.backgroundColorRes(R.color.white);
        if (title != null) {
            dialog.title(title);
//            dialog.title(title).titleColorRes(R.color.material_dialog_title_color);
        }
        if (content != null) {
            dialog.content(content);
//            dialog.content(content).contentColorRes(R.color.material_dialog_content_color);
        }
        if (negativeText != null) {
            dialog.negativeText(negativeText);
//            dialog.negativeText(negativeText)
//                    .negativeColorRes(R.color.material_dialog_cancel_color);
        }
        if (negtiveClickListener != null) {
            dialog.onNegative(negtiveClickListener);
        } else {
            dialog.onNegative(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.cancel();
                }
            });
        }
        if (positiveText != null) {
            dialog.positiveText(positiveText)
                    .positiveColorRes(R.color.colorPrimary);
        }
        if (positiveClickListener != null) {
            dialog.onPositive(positiveClickListener);
        } else {
            dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
                @Override
                public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                    dialog.dismiss();
                }
            });
        }
        dialog.cancelable(cancelable)
                .show();
    }


    public static void addDividerToRecyclerView(Context context, RecyclerView rv, boolean isVertical) {
//        int orientation = isVertical ? DividerItemDecoration.VERTICAL : DividerItemDecoration.HORIZONTAL;
//        rv.addItemDecoration(new DividerItemDecoration(context, orientation));
    }
}
